package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private ProjectPlanMapper projectPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingPlan> listAll() {
        return nursingPlanMapper.listAll();
    }

    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.add(nursingPlan);
        long id = nursingPlanMapper.getIdByname(nursingPlan.getPlanName());
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        for (NursingProjectPlanVo projectPlan : projectPlans) {
            projectPlan.setPlanId(id);
        }
        projectPlanMapper.addProjectPlans(projectPlans);
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 初始化分页插件，用于自动处理分页逻辑。
        PageHelper.startPage(pageNum, pageSize);
        // 执行分页查询，返回查询结果的Page对象。
        Page<NursingPlanVo> page = nursingPlanMapper.selectByPage(name, status);
        //获取count值
        for (NursingPlanVo nursingPlanVo : page) {
            Long id = nursingPlanVo.getId();
            nursingPlanVo.setCount(nursingLevelMapper.getCountByPlanId(id));
        }
        // 创建分页响应对象，并设置查询结果。
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public NursingPlanVo getById(long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        return BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
    }

    @Transactional
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        //拿到planId
        long id = nursingPlanDto.getId();
        //转换类型
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //更新数据
        nursingPlanMapper.update(nursingPlan);
        //删除计划对应的项目
        projectPlanMapper.deleteProjectPlan(id);
        //添加编辑后计划对应的项目
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        for (NursingProjectPlanVo projectPlan : projectPlans) {
            projectPlan.setPlanId(id);
        }
        projectPlanMapper.addProjectPlans(projectPlans);
    }

    @Override
    public void deleteById(long id) {
        //删除对应id的项目
        projectPlanMapper.deleteProjectPlan(id);
        //删除计划
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void changeStatus(long id, long status) {
        nursingPlanMapper.changeStatus(id, status);
    }
}
